package com.youth.framework.util;

import javax.mail.internet.AddressException;

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.log4j.Logger;

/**
 * common mail
 * 
 * @author <a href="mailto:adasai90@gmail.com">sayi</a>
 * 
 */
public class Mail {

	protected final Logger logger = Logger.getLogger(getClass());

	private String hostName;
	private String fromAddress;
	private int smtpPort;

	private String sslSmtpPort;
	private boolean sslOnConnect;

	private String userName;
	private String password;

	public Mail() {

	}

	private HtmlEmail getHtmlEmail() {
		HtmlEmail email = new HtmlEmail();
		try {
			email.setHostName(hostName);
			email.setSmtpPort(smtpPort);
			email.setSSLOnConnect(sslOnConnect);
			email.setSslSmtpPort(sslSmtpPort);
			email.setAuthenticator(new DefaultAuthenticator(userName, password));
			email.setFrom(fromAddress);
		} catch (EmailException e) {
			logger.error("email init failed: from address is invalid.");
		}
		return email;
	}

	/**
	 * 发送HTML格式化的消息
	 * 
	 * @param subject
	 *            主题
	 * @param htmlMsg
	 *            内容
	 * @param to
	 *            接收方
	 * @throws EmailException
	 * @throws AddressException
	 */
	public void sendHtmlMsg(String subject, String htmlMsg, String to) throws EmailException
			{
		HtmlEmail email = getHtmlEmail();
		email.setSubject(subject);
		email.setHtmlMsg(htmlMsg);
		email.addTo(to);
		email.send();
	}

	public void sendTextMsg(String subject, String msg, String to)
			throws EmailException {
		HtmlEmail email = getHtmlEmail();
		email.setSubject(subject);
		email.setTextMsg(msg);
		email.addTo(to);
		email.send();
	}

	/**
	 * @return the hostName
	 */
	public String getHostName() {
		return hostName;
	}

	/**
	 * @param hostName
	 *            the hostName to set
	 */
	public void setHostName(String hostName) {
		this.hostName = hostName;
	}

	/**
	 * @return the fromAddress
	 */
	public String getFromAddress() {
		return fromAddress;
	}

	/**
	 * @param fromAddress
	 *            the fromAddress to set
	 */
	public void setFromAddress(String fromAddress) {
		this.fromAddress = fromAddress;
	}

	/**
	 * @return the smtpPort
	 */
	public int getSmtpPort() {
		return smtpPort;
	}

	/**
	 * @param smtpPort
	 *            the smtpPort to set
	 */
	public void setSmtpPort(int smtpPort) {
		this.smtpPort = smtpPort;
	}

	/**
	 * @return the sslSmtpPort
	 */
	public String getSslSmtpPort() {
		return sslSmtpPort;
	}

	/**
	 * @param sslSmtpPort
	 *            the sslSmtpPort to set
	 */
	public void setSslSmtpPort(String sslSmtpPort) {
		this.sslSmtpPort = sslSmtpPort;
	}

	/**
	 * @return the sslOnConnect
	 */
	public boolean isSslOnConnect() {
		return sslOnConnect;
	}

	/**
	 * @param sslOnConnect
	 *            the sslOnConnect to set
	 */
	public void setSslOnConnect(boolean sslOnConnect) {
		this.sslOnConnect = sslOnConnect;
	}

	/**
	 * @return the userName
	 */
	public String getUserName() {
		return userName;
	}

	/**
	 * @param userName
	 *            the userName to set
	 */
	public void setUserName(String userName) {
		this.userName = userName;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password
	 *            the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

}
